package com.dd.admin.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dd.common.convert.SysMenuConvert;
import com.dd.common.dto.system.menu.SysMenuDTO;
import com.dd.common.dto.system.menu.SysMenuQueryDTO;
import com.dd.common.entity.SysMenu;
import com.dd.common.pojo.dto.group.CreateGroup;
import com.dd.common.pojo.dto.group.UpdateGroup;
import com.dd.common.service.SysMenuService;
import com.dd.common.utils.AjaxResult;
import com.dd.common.validator.annotation.IDMust;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author cheng
 */
@SaIgnore
@Tag(name = "菜单管理")
@Validated
@RequestMapping("/sys/menu")
@RestController
public class SysMenuController {
    @Resource
    private SysMenuService sysMenuService;

    @Resource
    private SysMenuConvert sysMenuConvert;

    @Operation(summary = "菜单新增", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
            description = "菜单数据",
            required = true,
            content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = SysMenuDTO.class)
            )))
    @PostMapping("/add")
    public AjaxResult<?> add(@Validated(CreateGroup.class) @RequestBody SysMenuDTO dto) {
        return AjaxResult.success(sysMenuService.save(sysMenuConvert.dtoToEntity(dto)));
    }

    @Operation(summary = "菜单编辑", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
            content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = SysMenuDTO.class))))
    @PatchMapping("/edit")
    public AjaxResult<?> edit(@Validated(UpdateGroup.class) @RequestBody SysMenuDTO dto) {
        return AjaxResult.success(sysMenuService.updateById(sysMenuConvert.dtoToEntity(dto)));
    }

    @Operation(summary = "菜单列表")
    @GetMapping("/list")
    public AjaxResult<?> list(@Validated SysMenuQueryDTO dto) {
        return AjaxResult.success(sysMenuService.list(new LambdaQueryWrapper<SysMenu>()
                .eq(dto.getParentId() != null, SysMenu::getParentId, dto.getParentId())
                .like(StringUtils.hasText(dto.getMenuName()), SysMenu::getMenuName, dto.getMenuName())
                .eq(dto.getMenuType() != null, SysMenu::getMenuType, dto.getMenuType())
                .like(StringUtils.hasText(dto.getPath()), SysMenu::getPath, dto.getPath())
                .like(StringUtils.hasText(dto.getPerms()), SysMenu::getPerms, dto.getPerms())
                .eq(dto.getStatus() != null, SysMenu::getStatus, dto.getStatus())
                .eq(dto.getVisible() != null, SysMenu::getVisible, dto.getVisible())
                .eq(dto.getIsCache() != null, SysMenu::getIsCache, dto.getIsCache())
        ));
    }

    @Operation(summary = "菜单删除", parameters = @Parameter(name = "id", in = ParameterIn.PATH, description = "菜单ID"))
    @DeleteMapping("/delete/{id}")
    public AjaxResult<Boolean> delete(@PathVariable @IDMust Long id) {
        return AjaxResult.success(sysMenuService.removeById(id));
    }

    @Operation(summary = "菜单详情", parameters = @Parameter(name = "id", in = ParameterIn.PATH, description = "菜单ID"))
    @GetMapping("/detail/{id}")
    public AjaxResult<?> detail(@PathVariable @IDMust Long id) {
        return AjaxResult.success(sysMenuService.getById(id));
    }

}
